package com.ruoyi.information.service.pc.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.pc.Cooperate;
import com.ruoyi.information.domain.pc.CooperateContent;
import com.ruoyi.information.mapper.pc.CooperateMapper;
import com.ruoyi.information.service.pc.ICooperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商务合作Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-14
 */
@Service
public class CooperateServiceImpl implements ICooperateService {
    @Autowired
    private CooperateMapper cooperateMapper;

    /**
     * 查询商务合作
     *
     * @return 商务合作
     */
    @Override
    public Cooperate selectCooperateById() {
        return cooperateMapper.selectCooperateById(SecurityUtils.getUserId());
    }

    /**
     * 查询商务合作列表
     *
     * @param cooperate 商务合作
     * @return 商务合作
     */
    @Override
    public List<Cooperate> selectCooperateList(Cooperate cooperate) {
        return cooperateMapper.selectCooperateList(cooperate);
    }

    /**
     * 新增商务合作
     *
     * @param cooperate 商务合作
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCooperate(Cooperate cooperate) {
        if (cooperate.getId() != null) {
            /*有id修改*/
            cooperate.setIsNew(1L);
            cooperate.setUpdateTime(DateUtils.getNowDate());
            cooperateMapper.deleteCooperateContentByCooperateId(cooperate.getId());
            insertCooperateContent(cooperate);
            return cooperateMapper.updateCooperate(cooperate);
        } else {
            /*无id新增*/
            Cooperate one = cooperateMapper.selectCooperateById(SecurityUtils.getUserId());
            if (one != null) {
                throw new RuntimeException("已存在但未传id");
            }
            cooperate.setContactsId(SecurityUtils.getUserId());
            cooperate.setCreateTime(DateUtils.getNowDate());
            int rows = cooperateMapper.insertCooperate(cooperate);
            insertCooperateContent(cooperate);
            return rows;
        }
    }

    /**
     * 修改商务合作
     *
     * @param cooperate 商务合作
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCooperate(Cooperate cooperate) {
        cooperate.setUpdateTime(DateUtils.getNowDate());
        cooperateMapper.deleteCooperateContentByCooperateId(cooperate.getId());
        insertCooperateContent(cooperate);
        return cooperateMapper.updateCooperate(cooperate);
    }

    /**
     * 批量删除商务合作
     *
     * @param ids 需要删除的商务合作主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCooperateByIds(Long[] ids) {
        cooperateMapper.deleteCooperateContentByCooperateIds(ids);
        return cooperateMapper.deleteCooperateByIds(ids);
    }

    /**
     * 删除商务合作信息
     *
     * @param id 商务合作主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCooperateById(Long id) {
        cooperateMapper.deleteCooperateContentByCooperateId(id);
        return cooperateMapper.deleteCooperateById(id);
    }

    /**
     * 新增商务合作记录信息
     *
     * @param cooperate 商务合作对象
     */
    public void insertCooperateContent(Cooperate cooperate) {
        List<CooperateContent> cooperateContentList = cooperate.getCooperateContentList();
        Long id = cooperate.getId();
        if (StringUtils.isNotNull(cooperateContentList)) {
            List<CooperateContent> list = new ArrayList<CooperateContent>();
            for (CooperateContent cooperateContent : cooperateContentList) {
                cooperateContent.setCooperateId(id);
                list.add(cooperateContent);
            }
            if (list.size() > 0) {
                cooperateMapper.batchCooperateContent(list);
            }
        }
    }
}
